// Learn TypeScript:
//  - https://docs.cocos.com/creator/manual/en/scripting/typescript.html
// Learn Attribute:
//  - https://docs.cocos.com/creator/manual/en/scripting/reference/attributes.html
// Learn life-cycle callbacks:
//  - https://docs.cocos.com/creator/manual/en/scripting/life-cycle-callbacks.html

import { game } from "../../../../script/game/control/game";
import { suduDiff, suduItemType, suduType } from "../../../../script/game/gameType";
import { ListViewAdapter } from "../../../../script/util/ListView/ListViewAdapter";
import { ccs } from "../../../../script/util/services";
import { WinBase } from "../../../../script/wins/WinBase";
import suduItemScript from "./suduItemScript";
import suduItem from "./suduItemScript";

const {ccclass, property} = cc._decorator;

@ccclass
export default class NewClass extends WinBase {

    @property(ListViewAdapter)
    list: ListViewAdapter = null;

    activeIdx: number = -1;
    activeNode: cc.Node = null;
    answer = [];
    puzzle = [];
    myAnswer = [];
    iq = 1;
    curNumber = -1; //当前的数字
    start () {
        this.iq = this._args.diffcult;
        //获取题目
        let data = ccs.dataMgr.getSuduData(suduDiff[this.iq]);
        this.puzzle = data.puzzle.split(',');
        this.answer = data.answer.split(',');
        this.myAnswer = this.puzzle.slice(0);

        this.showData();
        //输入答案
        this.nodes.select.children.forEach((val, idx) => {
            this.touchEndEventScale(val, () => {
                if(this.myAnswer[this.activeIdx] == idx + 1) return;
                if(this.activeNode && !this.puzzle[this.activeIdx]) {
                    this.myAnswer[this.activeIdx] = idx + 1;
                    this.curNumber = idx + 1;
                    this.showData();
                }
            })
        })
        //删除答案
        this.touchEndEventScale( this.nodes.delete, () => {
            if(!this.activeNode || this.puzzle[this.activeIdx]) {
                return ;
            }
            this.myAnswer[this.activeIdx] = "";
            this.curNumber = -1;
            this.showData();
        })
        this.touchEndEventScale(this.nodes.reStart, () => {
            ccs.winMgr.showWin("popAlert", {content: "确定重开吗？",cb: () => {
                this.myAnswer = this.puzzle.slice(0);
                this.curNumber = -1;
                this.activeIdx = -1;
                this.activeNode = null;
                this.showData();
            }})
        })
        this.touchEndEventScale(this.nodes.over, () => {
            ccs.winMgr.showWin("popAlert", {content: "确定提交吗？",cb: () => {
                if(this.myAnswer === this.answer) {
                    ccs.winMgr.showWin("toast", { msg: `恭喜回答正确，获得${this.iq}`})
                    game.recordMgr.addList({name: "你", action: `参加了智力风暴，拿下大奖`, effect: `iq+${this.iq}`});
                    game.playerMgr.changeEffect({iq: this.iq * 2}, 1);
                } else {
                    ccs.winMgr.showWin("toast", { msg: `答案错误，你将损失${this.iq}点iq`});
                    game.recordMgr.addList({name: "你", action: `参加了智力风暴，奈何智商不够，`,});
                }
                this._close();
            }})
        })
    }
    showData() {
        this.list.updateItems(this.myAnswer);
    }
    onCell(node: cc.Node, data: suduItemType, idx: number) {
        if(data == this.curNumber || idx == this.activeIdx) {
            node.getComponent(suduItemScript).setHigh();
        } else {
            node.getComponent(suduItemScript).unHigh();
        }
        //如果是本来的数字
        if(this.puzzle[idx]) {
            node.getComponent(suduItemScript).setOrigin();
        }
        this.touchEndEventScale(node, () => {
            if(this.activeIdx == idx ) return;
            if(this.activeIdx > 0 && data != this.curNumber) {
                this.activeNode.getComponent(suduItemScript).unHigh();
            }
            this.activeIdx = idx;
            this.activeNode = node;
            if(this.myAnswer[this.activeIdx]) {
                this.curNumber = this.myAnswer[this.activeIdx];
            } else {
                this.curNumber = -1;
            }
            node.getComponent(suduItemScript).setHigh();
            this.showData();
        })
    }
    getSameNode(idx: number) {
        let list = this.myAnswer.map(val => val == idx);
    }
}
